package com.hk.yingmi;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hk.Mgr.web.message.order.FundnavhistoryRes;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.entity.Fundnavhistory;
import com.hk.commons.utils.yingmi.YingmiApiUtils;
import com.hk.service.IFundnavHistory;

@Controller
public class FundnavhistoryCtr {

	private Logger logger = LoggerFactory.getLogger(FundnavhistoryCtr.class);

	@Autowired
	private YingmiApiUtils utils;
	@Autowired
	private IFundnavHistory fundnavhistoryService;

	/**
	 * 临时使用，系统上线后关闭
	 * 
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/saveFundhistory")
	public ResponseBase<Boolean> saveFundnavhistory() {
		ResponseBase<Boolean> rb = new ResponseBase<Boolean>();
		Map<String, String> params = new HashMap<>();
		/*
		 * String path = "/data/latest/fundnavhistory"; String result =
		 * utils.fileGet(path, params , "file-test.frontnode.net");
		 * List<String[]> list = new ArrayList<>();
		 * 
		 * list = Stream.of(result).map(s -> s.split("\r\n"))
		 * .collect(Collectors.toList()); String s[] = null; for (int i = 2; i <
		 * list.get(0).length; i++) { s =
		 * StringUtils.splitPreserveAllTokens(list.get(0)[i], '|');
		 * Fundnavhistory fnh = new Fundnavhistory();
		 * fnh.setId(Integer.parseInt(s[0])); fnh.setNavDate(s[1]);
		 * fnh.setFundCode(s[2]); fnh.setNav(!s[3].equals("") ? new
		 * BigDecimal(s[3]) : null); fnh.setAccumulatedNav(!s[4].equals("") ?
		 * new BigDecimal(s[4]) : null); fnh.setDayReturn(!s[5].equals("") ? new
		 * BigDecimal(s[5]) : null); fnh.setUnitYield(!s[6].equals("") ? new
		 * BigDecimal(s[6]) : null); fnh.setYearlyRoe(!s[7].equals("") ? new
		 * BigDecimal(s[7]) : null); if(fnh !=null)
		 * fundnavhistoryService.add(fnh); }
		 */
		String hql= "select DISTINCT fundCode from Fundnavhistory";
	
		List<String> list = fundnavhistoryService.executeListHql(hql);
		
		logger.info("当前基金数目为: " + list.size());
		logger.info("fundcode:{}" , list.get(1));
		params.put("fundCodes", list.get(1));
		String result = utils.post("/product/batchGetFundNav", params);
		ObjectMapper objectMapper = new ObjectMapper();
		List<LinkedHashMap<String, Object>> lhm = null;
		try {
			lhm = objectMapper.readValue(result, List.class);
			for (LinkedHashMap<String, Object> lkm : lhm) {
				logger.info(String.valueOf(lkm.get("nav")));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		rb.setMsg("成功");
		rb.setResultCode("0000");

		return rb;
	}

	@ResponseBody
	@RequestMapping(value = "/fundnavHisList", method = RequestMethod.POST)
	public ResponseBase<List<Fundnavhistory>> fundnavHisList(
			@RequestBody FundnavhistoryRes res) {
		ResponseBase<List<Fundnavhistory>> rb = new ResponseBase<>();
		try {
			if (logger.isDebugEnabled()) {
				logger.debug(
						"fundnavHisList params: fundCode:{} , pageNum:{} , pageSize:{}",
						res.getFundCode(), res.getCurPage(), res.getPageSize());
			}

			HqlFilter hqlFilter = new HqlFilter();
			hqlFilter.addEQFilter("fundCode", res.getFundCode());
			hqlFilter.addSort("navDate");
			hqlFilter.addOrder("DESC");
			
			List<Fundnavhistory> list = fundnavhistoryService.findByFilter(
					hqlFilter, res.getCurPage(), res.getPageSize());
			long size = fundnavhistoryService.countByFilter(hqlFilter);
			rb.setSize(size);
			rb.setResult(list);
		} catch (Exception e) {
			logger.error("/fundnavHisList , fundCode:{} , is error.{}",
					res.getFundCode(), e);
			rb.setResult(null);
		}

		return rb;
	}

}
